﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.ComponentModel;

namespace cs2335lab5
{
    namespace Client
    {
        /// <summary>
        /// This helps to sort a List View
        /// This is from http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-2-sorting
        /// </summary>
        public class SortAdorner : Adorner
        {
            // At the top, we have two static Geometries:
            // one for the up arrow
            private readonly static Geometry _AscGeometry = 
                Geometry.Parse("M 0,0 L 10,0 L 5,5 Z");

            // and one for the down arrow
            private readonly static Geometry _DescGeometry =
                Geometry.Parse("M 0,5 L 10,5 L 5,0 Z");

            // sort direction
            public ListSortDirection Direction { get; private set; }

            // The UIElement will be the element that the adorner will adorn
            public SortAdorner(UIElement element, ListSortDirection dir)
                : base(element)
            { 
                Direction = dir; 
            }

            /// <summary>
            /// All of the rendering occurs in this function
            /// </summary>
            /// <param name="drawingContext"></param>
            protected override void OnRender(DrawingContext drawingContext)
            {
                base.OnRender(drawingContext);

                /// First, if the column header is less than 20 pixels wide, 
                /// we don't bother drawing the arrow (it looks kind of silly). 
                if (AdornedElement.RenderSize.Width < 20)
                    return;

                /// Then we push a TranslateTransform onto the drawing context 
                /// transform stack. This makes sure that the arrow will be 
                /// drawn at the right edge of the column and vertically in the 
                /// center. 
                drawingContext.PushTransform(
                    new TranslateTransform(
                      AdornedElement.RenderSize.Width - 15,
                      (AdornedElement.RenderSize.Height - 5) / 2));

                /// In the next line, we draw the geometry (looking at 
                /// the Direction to figure out which one)
                drawingContext.DrawGeometry(Brushes.Black, null,
                    Direction == ListSortDirection.Ascending ?
                      _AscGeometry : _DescGeometry);

                /// Finally, we pop the translate transform off of the transform 
                /// stack (just to keep the transform stack clean).
                drawingContext.Pop();
            }
        }
    }
}